package com.gmail.bloim.serg.testrunner;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Collections;
import java.util.List;

/**
 * User: Администратор
 * Date: 25.04.13
 * Time: 11:34
 */
public class Test {
    private static final InputStreamCreator EMPTY_INPUT_STREAM_CREATOR = new InputStreamCreator() {
        public final byte[] EMPTY_BYTE_ARRAY = new byte[0];

        @Override
        public InputStream createInputStream() throws FileNotFoundException {
            return new ByteArrayInputStream(EMPTY_BYTE_ARRAY);
        }
    };
    private final String name;
    private final InputStreamCreator stdIn;
    private final InputStreamCreator res;
    private final String[] args;

    public Test(File stdIn, File stdOut, File argsFile) {
        InputStreamCreator stdIn1;
        String nameTmp;
        boolean in = false, out = false, args = false;
        try {
            nameTmp = FilenameUtils.getBaseName(stdIn.getName());
        } catch (Exception e) {
            nameTmp = "test";
        }
        this.name = nameTmp;
        try {
            stdIn1 = new FileInputStreamCreator(stdIn);
            in=true;
        } catch (Exception e) {
            stdIn1 = EMPTY_INPUT_STREAM_CREATOR;
        }
        this.stdIn = stdIn1;
        InputStreamCreator res;
        try {
            res = new FileInputStreamCreator(stdOut);
            out=true;
        } catch (Exception e) {
            res = EMPTY_INPUT_STREAM_CREATOR;
        }
        this.res = res;

        String[] argsTmp;
        try {
            argsTmp = IOUtils.readLines(new FileInputStream(argsFile)).toArray(new String[0]);
            args = true;
        } catch (Exception e) {
            argsTmp = new String[0];
        }
        this.args = argsTmp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Test)) return false;

        Test test = (Test) o;

        if (name != null ? !name.equals(test.name) : test.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    public String[] getArgs() {
        return args;
    }

    public InputStream createStdIn() throws FileNotFoundException {
        return stdIn.createInputStream();
    }

    public InputStream createStdOut() throws FileNotFoundException {
        return res.createInputStream();
    }

    public String getName() {
        return name;
    }

    private static class FileInputStreamCreator implements InputStreamCreator {
        private final File file;

        public FileInputStreamCreator(File file) {
            this.file = file;
        }

        @Override
        public InputStream createInputStream() throws FileNotFoundException {
            return new FileInputStream(file);
        }
    }

    private class FileOutputStreamCreator implements OutputStreamCreator {
        private final File file;

        public FileOutputStreamCreator(File file) {
            this.file = file;
        }

        @Override
        public OutputStream createOutputStream() throws FileNotFoundException {
            return new FileOutputStream(file);
        }
    }
}
